<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/13
 * Time: 22:10
 */
namespace app\index\controller;

use app\BaseController;

use app\index\logic\LeaveLogic;
use think\facade\Request;

class Leave extends BaseController
{
    /*
     * 请假
     */
    public function create(){
        $leaveLogic = new LeaveLogic();
        success($leaveLogic->doLeave());
    }
    /*
     * 我发起的
     */
    public function startList(){
        $leaveLogic = new LeaveLogic();
        success($leaveLogic->getStartList());
    }

    /*
     * 待我审批的
     */
    public function approvalList(){
        $leaveLogic = new LeaveLogic();
        success($leaveLogic->getApprovalList());
    }

    /*
     * 请假详情
     */
    public function read(){

        $leaveId = Request::get('leaveId');
        if(!$leaveId)   fail('请选择请假记录');
        $leaveLogic = new LeaveLogic();
        success($leaveLogic->getInfo($leaveId));
    }

    /*
     * 撤回
     */
    public function remove(){
        $leaveId = Request::post('leaveId');
        if(!$leaveId)   fail('请选择请假记录');

        $leaveLogic = new LeaveLogic();
        success($leaveLogic->remove($leaveId));
    }

    /*
     * 提交审批结果
     */
    public function approval(){

        $param = Request::post();
        if(!isset($param['leaveId']) || empty($param['leaveId']))   fail('请选择请假记录');
        $leaveId = intval($param['leaveId']);

        if(!isset($param['approval']) || empty($param['approval']))   fail('请选择通过或者拒绝');
        $approval = intval($param['approval']);

        $reason = '';
        if($approval == \app\model\Leave::APPROVAL_REJECT){
            //拒绝 需要填写原因
            if(!isset($param['reason']) || empty($param['reason']))   fail('请填写原因');
            $reason = trim($param['reason']);
        }

        $leaveLogic = new LeaveLogic();
        success($leaveLogic->approval($leaveId,$approval,$reason));
    }
}